Component({
    properties: {
        placeholder: {
            type: String,
            value: ''
        },
        type: {
            type: String,
            value: 'text'
        },
        confirmText: {
            type: String,
            value: '确认'
        },
        confirmColor: {
            type: String,
            value: '#f88e2d'
        },
        cancelText: {
            type: String,
            value: '取消'
        },
        cancelColor: {
            type: String,
            value: '#eee'
        },
        title: {
            type: String,
            value: ''
        },
        value: {
            type: String,
            value: ''
        },
        show: {
            type: Boolean,
            value: false
        },
    },
    options: {
        addGlobalClass: true
    },
    externalClasses: ['lk-class'],
    data: {},
    confirm() {
    },
    cancel() {
    },
    methods: {
        show({confirm, cancel, title, placeholder}) {
            if (title) {
                this.setData({
                    title
                })
            }
            if (placeholder) {
                this.setData({
                    placeholder
                })
            }
            this.confirm = confirm
            this.cancel = cancel
            if (confirm && typeof confirm === 'function') {
                this.confirm = confirm
            }
            if (cancel && typeof cancel === 'function') {
                this.cancel = cancel
            }
            this.toggleModal(true)
        },
        handlerClose() {
            this.toggleModal(false)
        },
        handlerMaskCloseModal() {
            if (!this.data.mask) {
                return
            }
            this.toggleModal(false)
        },
        toggleModal(show = true) {
            this.setData({
                show
            })
        },
        async handlerConfirm() {
            let close = true
            if (typeof this.confirm === 'function') {
                close = await this.confirm(this.data.value)
            }
            this.triggerEvent('confirm', {value: this.data.value})
            if (close === false) {
                return
            }
            this.toggleModal(false)
        },
        async handlerCancel() {
            let close = true
            if (typeof this.cancel === 'function') {
                close = await this.cancel(this.data.value)
            }
            if (close === false) {
                return
            }
            this.toggleModal(false)
        }
    }
})
